// 2、无头双向链表实现
public class MyLinkedList {
    class LinkedList {
        public int val;
        public LinkedList prev;
        public LinkedList next;
        public LinkedList(int val) {
            this.val = val;
        }
    }
    public  LinkedList head;
    public  LinkedList last;
    //头插法
    public void addFirst(int data) {
        LinkedList node = new LinkedList(data);
        if (head == null) {
            head = last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        LinkedList node = new LinkedList(data);
        if (last == null) {
            head = last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        LinkedList node = new LinkedList(data);
        try {
            checkIndex(index);
        }catch (indexNotLegalException e) {
            e.printStackTrace();
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        LinkedList cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }
    private void checkIndex(int index) {
        if(index < 0 || index > size()) {
            throw new indexNotLegalException("双向链表插入index位置不合法: "+index);
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        LinkedList cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;

    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) {
            System.out.println("链表没有数据无法删除！");
            return;
        }
        if (head.val == key){
            head = head.next;
            if (head == null) {
                last = null;
            }else {
                head.prev = null;
            }
            return;
        }
        LinkedList cur =head;
        while (cur.next != null) {
            if (cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }
        if (last.val == key) {
            last = last.prev;
            last.next = null;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head == null) {
            System.out.println("链表没有数据无法删除！");
            return;
        }
        while (head != null && head.val == key){
            head = head.next;
            if (head == null) {
                last = null;
            } else {
                head.prev = null;
            }
        }
        LinkedList cur =head;
        while (cur != null && cur.next != null) {
            if (cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
        if (last != null && last.val == key) {
            last = last.prev;
            last.next = null;
        }
    }
    //得到单链表的长度
    public int size() {
        LinkedList cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur =cur.next;
        }
        return count;
    }
    public void display() {
        LinkedList cur = head;
        if (head == null) {
            System.out.println("");
            return;
        }
        for (int i = 0; i < size() - 1; i++) {
            System.out.print(cur.val+" <——> ");
            cur = cur.next;
        }
        System.out.print(cur.val);
    }
    public void clear() {
        head = null;
        last = null;
    }
}